﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace InSafetyAirClient.GUI.Airport
{
    public partial class UpdateAip : UserControl
    {
        List<InSafetyAirClient.InSafetyAirService.Airport> aips = null;

        public UpdateAip()
        {
            InitializeComponent();
        }

        private void UpdateAip_Load(object sender, EventArgs e)
        {
            if (!this.DesignMode)
            {
                populateList();
            }
        }

        private void populateList()
        {
            AutoCompleteStringCollection autoComplete = new AutoCompleteStringCollection();
            ServiceSingle servS = ServiceSingle.Instance;
            //
            //Search reg
            //
            aips = servS.getService().GetAll(typeof(InSafetyAirClient.InSafetyAirService.Airport).Name).OfType<InSafetyAirClient.InSafetyAirService.Airport>().ToList();
            foreach (InSafetyAirService.Airport air in aips)
            {
                comboBox1.Items.Add(air.name);
                autoComplete.Add(air.name);
            }
            comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
            comboBox1.AutoCompleteCustomSource = autoComplete;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            foreach (InSafetyAirService.Airport air in aips)
            {
                if (air.name.Equals(comboBox1.Text))
                {
                    textBox1.Text = air.name;
                    textBox3.Text = air.town;
                    numericUpDown1.Value =(decimal) air.gmt;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (InSafetyAirService.Airport air in aips)
            {
                if (air.name.Equals(comboBox1.Text))
                {
                    air.name = textBox1.Text;
                    air.town = textBox3.Text;
                    air.gmt = (byte)numericUpDown1.Value;
                    ServiceSingle.Instance.getService().AddToDatabase(air);
                }
            }
        }
    }
}
